package com.ocm.tourism.webclient.service;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.ocm.tourism.core.dao.AuditInfoDao;
import com.ocm.tourism.core.domain.AuditBusinessAdvs;
import com.ocm.tourism.core.model.ImageItem;
import com.ocm.tourism.picasaweb.service.ImageUploaderService;
import com.ocm.tourism.webclient.model.WebClientModel;

public class WebClientManager {

   private List<WebClientModel> webClientModels;

   private List<ImageItem> mainImageItems;

   private Date lastModifiedTime;

   private static WebClientManager webClientManager;

   private Date lastRetreiveHotelAdvTime; 

   public synchronized List<ImageItem> getMainImageItems(ImageUploaderService imageUploaderService, String mainAlbumId){
      if (mainImageItems == null){
         mainImageItems = imageUploaderService.retriveFiles(mainAlbumId);
      }
      return mainImageItems;
   }

   public synchronized List<WebClientModel> getClientModels(List<WebClientService> webClientServices){
      if (hasReset()){
         List<WebClientModel> webClientModels = new ArrayList<WebClientModel>();
         for (WebClientService webClientService : webClientServices){
            List<WebClientModel> news = webClientService.getWebClientModels();
            if (news != null){
               webClientModels.addAll(news);
            }
         }
         this.webClientModels = webClientModels;
         this.lastModifiedTime = new Date(System.currentTimeMillis());
      }
      return this.webClientModels;
   }

   public synchronized boolean hasUpdateAdv(){
      if (lastRetreiveHotelAdvTime == null)
         return true;

      Calendar modifiedTime = Calendar.getInstance();
      modifiedTime.setTime(lastRetreiveHotelAdvTime);

      long amount = System.currentTimeMillis() - modifiedTime.getTimeInMillis();
      final long msPerHour = 1000L * 60L; 
      int minutes = (int) (amount / msPerHour);

      if (minutes > 5){
         return true;
      }
      return false;
   }

   public synchronized void getNextIndex(AuditBusinessAdvs auditBusinessAdvs,AuditInfoDao auditInfoDao){
       if (WebClientManager.instance().hasUpdateAdv()){
          this.lastRetreiveHotelAdvTime = new Date(System.currentTimeMillis());
          auditBusinessAdvs.setLastIndex(auditInfoDao.getNextIndex(auditBusinessAdvs.getTransalationType()));
       }
       auditInfoDao.saveAuditBusinessAdvs(auditBusinessAdvs);
   }

   public boolean hasReset(){
      if (lastModifiedTime == null)
         return true;

      Calendar modifiedTime = Calendar.getInstance();
      modifiedTime.setTime(lastModifiedTime);
      int modifiedHours = modifiedTime.get(Calendar.HOUR_OF_DAY);
      
      Date currentDate = new Date(System.currentTimeMillis());
      Calendar currentTime = Calendar.getInstance();
      currentTime.setTime(currentDate);
      int currentHours = currentTime.get(Calendar.HOUR_OF_DAY); 
      if (currentHours >= 8 && modifiedHours <= 7){
         return true;
      }

      if (currentHours >= 14 && modifiedHours <= 13){
         return true;
      }

      return false;
   }

   public static WebClientManager instance(){
      if (webClientManager == null){
         synchronized (WebClientManager.class) {
            if (webClientManager == null){
               webClientManager = new WebClientManager();
            }
         }
      }
      return webClientManager;
   }
}
